#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif
#ifdef __APPLE__
#include <SDL/SDL.h>
#else
#include <SDL.h>
#endif

#define PPM 50
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define DEBUG 0 //david sucks balls

#include <Box2d/Box2d.h>
#include "include/sdl_world.h"
#include "include/drawable.h"
#include "include/ball.h"
#include "include/input.h"
#include "include/staticshape.h"

int main ( int argc, char** argv )
{
    /*
    You have to come up with a reasonable conversion factor. I suggest making this choice based on the size of your characters.
    Suppose you have determined to use 50 pixels per meter (because your character is 75 pixels tall).
    Then you can convert from pixels to meters using these formulas:

    xMeters = 0.02f * xPixels;
    yMeters = 0.02f * yPixels;

    In reverse:

    xPixels = 50.0f * xMeters;
    yPixels = 50.0f * yMeters;
*/
    freopen( "CON", "wt", stdout );
    freopen( "CON", "wt", stderr );
    printf("Starting!\n");

    //Instantiate the world
    b2Vec2 gravity(0.0f, 9.8);
    bool doSleep = true;
    b2World world(gravity, doSleep);


    //Create the ground
    b2BodyDef groundBodyDef;
    groundBodyDef.position.Set(0.0f, 0.02f * 480);
    b2Body* groundBody = world.CreateBody(&groundBodyDef);
    b2PolygonShape groundBox;
    groundBox.SetAsBox((0.02 * 640), (0.02 * 32));
    groundBody->CreateFixture(&groundBox,0.0f);

    //Create the ball with a radius of 1
    b2CircleShape shape;
    shape.m_radius = .02 * 20;

    b2FixtureDef fd;
    fd.shape = &shape;
    fd.density = 1.0f;

    b2BodyDef bd;
    bd.type = b2_dynamicBody;
    bd.position.Set(0.02 * 240, 0.02 * 100);

    b2Body* body = world.CreateBody(&bd);

    fd.restitution = 0.4f;
    body->CreateFixture(&fd);


    //Create a new world
    SDL_World* myGFXWorld = new SDL_World(640,480,60);

    /*Run the main loop*/
    float32 timeStep = 1.0f / 60.0f;
    int32 velocityIterations = 6;
    int32 positionIterations = 2;

    //Create a ball
    Ball* myBall = new Ball();
    myGFXWorld->AddToScene(myBall);

    //Create the ground
    StaticShape* ground = new StaticShape(StaticShape::Rectangle,0,448,640,32);
    myGFXWorld->AddToScene(ground);

    //Handle Input
    Input* inputHandler = new Input();


    //Main game loop
    while(true)
    {
        //Input Update Draw

        // Handle input
      inputHandler->readInput();

      if (inputHandler->windowClosed())
      {
         return 0;
      }

        bool* keysHeld = inputHandler->getInput();

        if (keysHeld[SDLK_UP])
        {
            b2Vec2 imp;
            imp.x = 0;
            imp.y -=.005;
            body->ApplyLinearImpulse(imp,body->GetPosition());


        }
        if(keysHeld[SDLK_RIGHT])
        {
            b2Vec2 imp;
            imp.x += 0.01;
            imp.y = 0;
            body->ApplyForce(imp,body->GetPosition());
        }

        if(keysHeld[SDLK_LEFT])
        {
            b2Vec2 imp;
            imp.x -= 0.01;
            imp.y = 0;
            body->ApplyForce(imp,body->GetPosition());
        }



        world.Step(timeStep, velocityIterations, positionIterations);
        world.ClearForces();
        b2Vec2 position = body->GetPosition();
        float32 angle = body->GetAngle();
        printf("%4.2f %4.2f %4.2f\n", position.x, position.y, angle);

        myBall->UpdatePosition(position.x*PPM, position.y*PPM);
//        (Ball*)myBall->UpdatePosition(position.x*PPM, position.y*PPM);
        myGFXWorld->Draw();

    }
}
